﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace ADayRoiAdmin
{
    public partial class frmImageLink : Form
    {
        static SqlConnection con = null;
        private BindingManagerBase bm = null;
        private string temp_picture = string.Empty;
        AdayroiClass adayroi = new AdayroiClass(ref con);
        DataTable dt_listHinhAnh;
        public frmImageLink()
        {
            InitializeComponent();
        }

        private void frmImageLink_Load(object sender, EventArgs e)
        {
            dgv_ImagesListLoad();
            string sql = "select * from CategoryLinks";
            dt_listHinhAnh = adayroi.getList(sql,con);
            cbDanhMuc.DataSource = dt_listHinhAnh;
            cbDanhMuc.ValueMember = "PKID";
            cbDanhMuc.DisplayMember = "CategoryName";
            cbDanhMuc.SelectedIndex = 0;
        }

        private void cbDanhMuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListHinhAnhLoad();
        }

        private void ListHinhAnhLoad()
        {
            try
            {
                string sql = "select * from ImagesLinks where CategoryLinks = " + cbDanhMuc.SelectedValue.ToString();
                DataTable dt_ImagesList = adayroi.getList(sql,con);
                dgv_listHinhAnh.DataSource = dt_ImagesList.DefaultView;
                bm = dgv_listHinhAnh.BindingContext[dgv_listHinhAnh.DataSource, dgv_listHinhAnh.DataMember];
            }
            catch { }
        }

        private void dgv_ImagesListLoad()
        {
            DataGridViewTextBoxColumn PKID = new DataGridViewTextBoxColumn();
            PKID.DataPropertyName = "PKID";
            PKID.Name = "PKID";
            PKID.Visible = false;

            DataGridViewTextBoxColumn Images = new DataGridViewTextBoxColumn();
            Images.DataPropertyName = "Images";
            Images.FillWeight = 30F;
            Images.Name = "Images";

            DataGridViewTextBoxColumn Links = new DataGridViewTextBoxColumn();
            Links.DataPropertyName = "Links";
            Links.HeaderText = "Link";
            Links.Name = "Links";

            DataGridViewTextBoxColumn CategoryLinks = new DataGridViewTextBoxColumn();
            CategoryLinks.DataPropertyName = "CategoryLinks";
            CategoryLinks.Name = "CategoryLinks";
            CategoryLinks.Visible = false;

            DataGridViewTextBoxColumn Sort = new DataGridViewTextBoxColumn();
            Sort.DataPropertyName = "Sort";
            Sort.Name = "Sort";
            Sort.Visible = false;

            DataGridViewCheckBoxColumn IsDisplay = new DataGridViewCheckBoxColumn();
            IsDisplay.DataPropertyName = "IsDisplay";
            IsDisplay.Name = "IsDisplay";
            IsDisplay.HeaderText = "Display";


            dgv_listHinhAnh.Columns.AddRange(new DataGridViewColumn[] { PKID, Images, Links, CategoryLinks, Sort, IsDisplay });
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            string picture = adayroi.get_path_pic();
            temp_picture = adayroi.loadpicture(picture, pictureBox1);
        }

        private void dgv_listHinhAnh_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataRow findRow = ((DataRowView)bm.Current).Row;
            if (findRow["Images"].ToString() != String.Empty)
                adayroi.lay_hinh
                    ("/Code/Upload/Links/" + findRow["Images"].ToString(), pictureBox1);
            txtDoUuTien.Text = findRow["Sort"].ToString();
            txtLink.Text = findRow["Links"].ToString();
            chkhienthi.Checked = (bool)findRow["isDisplay"];
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (adayroi.ftp_picture(temp_picture, "/Code/Upload/Links/" + Path.GetFileName(temp_picture)) == 0)
            {
                string sql = "insert into ImagesLinks (Images,Links,CategoryLinks,IsDisplay,Sort) values ('" + Path.GetFileName(temp_picture) + "','" + txtLink.Text +
                    "'," + cbDanhMuc.SelectedValue.ToString() + ",'" + chkhienthi.Checked + "'," + txtDoUuTien.Text + ")";
                if(adayroi.UpdateSQL(con, sql)==0)
                    MessageBox.Show("Thành Công");
                ListHinhAnhLoad();
            }
            else
            {
                MessageBox.Show("Upload Hình bị lỗi, vui lòng thử lại một lần nữa");
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
             DataRow findRow = ((DataRowView)bm.Current).Row;

             if (!String.IsNullOrEmpty(temp_picture) && Path.GetFileName(temp_picture) != findRow["Images"].ToString())
                adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/Links/", findRow["Images"].ToString());

            if (adayroi.ftp_picture(temp_picture, "/Code/Upload/Links/" + Path.GetFileName(temp_picture)) == 0)
            {
                string str_pic = Path.GetFileName(temp_picture);
                if (str_pic == "") str_pic = findRow["Images"].ToString();
                string sql = "Update ImagesLinks set Images = '" + str_pic
                    + "', Links = '" + txtLink.Text + "', CategoryLinks = " + cbDanhMuc.SelectedValue.ToString() + ", IsDisplay = '" + chkhienthi.Checked
                    + "', Sort = " + txtDoUuTien.Text + " where PKID = " + findRow["PKID"].ToString();
                if (adayroi.UpdateSQL(con, sql) == 0)
                    MessageBox.Show("Thành Công");
                ListHinhAnhLoad();
            }
            else {
                MessageBox.Show("Upload Hình bị lỗi, vui lòng thử lại một lần nữa");
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DataRow findRow = ((DataRowView)bm.Current).Row;
            if (adayroi.deleteFile("ftp://buyall.vn/httpdocs", "/Code/Upload/Links/", findRow["Images"].ToString()) == 0)
            {
                string sql = "delete from ImagesLinks where PKID = " + findRow["PKID"].ToString();
                if (adayroi.UpdateSQL(con, sql) == 0)
                    MessageBox.Show("Thành Công");
                ListHinhAnhLoad();
            }
            else
            {
                MessageBox.Show("Upload Hình bị lỗi, vui lòng thử lại một lần nữa");
            }
        }


    }
}
